package clue.editor;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

import objects.BoardData;

public class ClueFrame extends JFrame {
	private static final long serialVersionUID = 1L;

	private static ClueFrame clueFrame;
	
	private BoardPanel boardPanel;
	private UndoManager undoManager;
	
	public static ClueFrame getInstance()
	{
		if (clueFrame == null)
		{
			clueFrame = new ClueFrame();
		}
		
		return clueFrame;
	}
	
	/**
	 * Clue frame private ctor 
	 */
	private ClueFrame() {

		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		setLayout(new CardLayout());
		
		setJMenuBar(new ClueMenu());

		setTitle("The Clue"); // set the frame's title

		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		
		// set the frame's Close operation
		addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				
				int choice = JOptionPane.OK_OPTION;
				
				if (GlobalHistory.getInstance().isUndoable())
				{
					choice = JOptionPane.showConfirmDialog(clueFrame, "You have done changes. All changes will be lost! do you want to continue?");
				}
				
				if (choice == JOptionPane.OK_OPTION)
				{
					System.exit(0);
				}
			}
		});

		//Set size
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = new Dimension();
		frameSize.setSize(screenSize.width, screenSize.height * 0.95);
		setSize(frameSize);
		setLocationRelativeTo(null);
		setVisible(true);

		// Initializing undo manager
		undoManager = new UndoManager();
		
		// Initializing board panel that holds the main panel for the editor
		boardPanel = new BoardPanel();
		add(boardPanel);
		
		BoardData boardData = new BoardData();
		boardData.setEnvelope(null);
		boardData.setPlayers(null);
		
		// Save the start state of the board using board data
		GlobalHistory.getInstance().setPreviousVersion(boardData);
	}

	/**
	 * The method is adding an edit to the history so we can use it for undo/redo
	 */
	public void addEdit()
	{
		GlobalHistory.getInstance().addEdit(BoardDataParser.parseFrameToBoardData());
		((ClueMenu)ClueFrame.getInstance().getJMenuBar()).getRedo().setEnabled(GlobalHistory.getInstance().isRedoable());
		((ClueMenu)ClueFrame.getInstance().getJMenuBar()).getUndo().setEnabled(GlobalHistory.getInstance().isUndoable());
	}
	
	/**
	 * The method set a message in the message label for the user
	 * @param errorText
	 * @param color
	 */
	public void setMessage(String errorText, Color color)
	{
		boardPanel.getPlayersPanel().changeErrorText(errorText, color);
	}
	
	public BoardPanel getBoardPanel() {
		return boardPanel;
	}
	
	public PlayersPanel getPlayersPanel() {
		if (boardPanel == null)
		{
			return null;
		}
		
		return boardPanel.getPlayersPanel();
	}

	public void setBoardPanel(BoardPanel boardPanel) {
		this.boardPanel = boardPanel;
	}

	public UndoManager getUndoManager() {
		return undoManager;
	}
	
	

	
}
